package my.learn.tx.onAnno.dao.impl;

import my.learn.tx.onAnno.dao.AccountDao;
import my.learn.tx.onAnno.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AccountDaoImpl implements AccountDao {  // extends JdbcDaoSupport

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Account findById(Integer accountId) {

        List<Account> accounts = jdbcTemplate.query("select * from account where id=?", new BeanPropertyRowMapper<Account>(Account.class), accountId);
        return accounts.isEmpty()?null:accounts.get(0);
    }

    @Override
    public Integer updateAccount(Account account) {

        return jdbcTemplate
                .update("update account set name= ?, money = ? where id= ?", account.getName(), account.getMoney(), account.getId());
    }
}
